/**
 *It is a demo for practicing JAVA. All codes are opened source by Jian.
 */
package jian.tool.daocode.util;

import java.util.Set;

/**
 * FlashUtil代码生成
 * 
 * @author Jian
 * @date 2013-4-17
 * @version
 * 
 */
public class CodeFlashUtilHelper {

	/**
	 * 生成 FlahUtil.java 中的代码
	 * 
	 * @param entityName
	 * @param properties
	 * @return
	 */
	public static String generateFlashUtilCode(String entityName,
			final Set<String> properties) {

		StringBuffer sb = new StringBuffer(
				"/**\n* @see I am auto generated by Jian\n*/\n");
		sb.append("public static Element create");
		sb.append(entityName);
		sb.append("(Element element, ");
		sb.append(entityName);
		sb.append("Entity entity)\n{\n");
		sb.append("\tElement result = element.addElement(\"item\");\n");
		sb.append("\ttry\n\t{\n");
		for (String property : properties) {
			sb.append("\t\taddAttribute(result, \"");
			sb.append(property);
			sb.append("\", entity.get");
			sb.append(Util.changeFirstToUpperCase(property));
			sb.append("());\n");
		}

		sb.append("\t}\n\tcatch (Exception e)\n\t{\n\tLOGGER.error(\"Create ");
		sb.append(entityName);
		sb.append("Error!~\", e);\n\t}\n\n\treturn result;\n");
		sb.append("}");
		return sb.toString();
	}
}
